using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace stadt_verlangen
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Main : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;

        public const int screenWidth = 1024;
        public const int screenHeight = 768;

        Texture2D backgroundTexture;
        Texture2D[] contentSpinGame;
        Texture2D[] subMenuSprites;
        SoundEffect backGroundMusik;
        SoundEffect cursorSound;
        SoundEffectInstance soundEngineInstance;
        SpriteFont gameFont;
        SpriteFont scoreFont;
        Rectangle viewportRect;
        GameButtons[] buttons;
        int menuState;
        int activebutton;
        int maxButtons;
        KeyboardState keyboardState = Keyboard.GetState();
        KeyboardState previousKeyboardState;
        bool isGameRunning;
        bool loopMusik;
        bool escapeLock;
        bool enterLock;
        SpriteBatch spriteBatch;
        SubMenu1 subMenu1;
        GameShell gameShell;

        public Main()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = screenWidth;
            graphics.PreferredBackBufferHeight = screenHeight;
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            maxButtons = 3;
            menuState = 2;
            buttons = new GameButtons[maxButtons];
            contentSpinGame = new Texture2D[2];
            subMenuSprites = new Texture2D[5];
            activebutton = 0;
            isGameRunning = false;
            loopMusik = false;
            escapeLock = true;
            enterLock = true;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            backgroundTexture = Content.Load<Texture2D>("Sprites\\map_test");
            subMenuSprites[0] = Content.Load<Texture2D>("Sprites\\Menu\\Submenu_1\\interface1_1");
            subMenuSprites[1] = Content.Load<Texture2D>("Sprites\\Menu\\Submenu_1\\interface1_2_test");
            subMenuSprites[2] = Content.Load<Texture2D>("Sprites\\Menu\\Submenu_1\\startbutton");
            subMenuSprites[3] = Content.Load<Texture2D>("Sprites\\Menu\\Submenu_1\\anleitungbutton");
            subMenuSprites[4] = Content.Load<Texture2D>("Sprites\\Menu\\Submenu_1\\abbrechenbutton");
            buttons[0] = new GameButtons(Content.Load<Texture2D>("Sprites\\baeckerei_test"), new Vector2(680.0f, 405.0f), 1, new int[] { 0, 1000, 500, 200 }, "Loewenbaeckerei");
            buttons[0].avaiable = true;
            buttons[0].selected = true;
            buttons[1] = new GameButtons(Content.Load<Texture2D>("Sprites\\baeckerei_test"), new Vector2(450.0f, 380.0f), 2, new int[] { 0, 1000, 500, 200 }, "Test2");
            buttons[1].avaiable = true;
            buttons[2] = new GameButtons(Content.Load<Texture2D>("Sprites\\baeckerei_test"), new Vector2(250.0f, 330.0f), 2, new int[] { 0, 1000, 500, 200 }, "Test3");
            buttons[2].avaiable = true;

            viewportRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
            gameFont = Content.Load<SpriteFont>("Fonts\\GameFont");
            scoreFont = Content.Load<SpriteFont>("Fonts\\ScoreFont");

            backGroundMusik = Content.Load<SoundEffect>("Sounds\\la_mer_loop1");
            cursorSound = Content.Load<SoundEffect>("Sounds\\Cursor1");

            //Load Content for SpinGame

            contentSpinGame[0] = Content.Load<Texture2D>("Sprites\\map_dreh_test");
            contentSpinGame[1] = Content.Load<Texture2D>("Sprites\\figur_test");

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            //Update Logik for MapMenu
            if (!isGameRunning)
            {

                previousKeyboardState = keyboardState;
                keyboardState = Keyboard.GetState();

                //HintergrundMusik

                if (soundEngineInstance == null)
                {
                    soundEngineInstance = backGroundMusik.Play(0.5f, 0.0f, 0.0f, true);
                    loopMusik = true;
                }
                if (!loopMusik)
                {
                    soundEngineInstance.Play();
                    loopMusik = true;
                }

                for (int i = 0; i < buttons.Length; i++)
                {
                    buttons[i].update(gameTime);

                }

                if (!(keyboardState.IsKeyDown(Keys.Enter) || keyboardState.IsKeyDown(Keys.Space)) && enterLock) 
                {
                    enterLock = false;
                }

                if (!keyboardState.IsKeyDown(Keys.Escape) && escapeLock) 
                {
                    escapeLock = false;
                }

                if (menuState == 2)
                {

                    if (keyboardState.IsKeyDown(Keys.Escape)&& !escapeLock)
                    {
                        this.Exit();
                    }

                    if (keyboardState.IsKeyDown(Keys.Left))
                    {
                        if (!previousKeyboardState.IsKeyDown(Keys.Left))
                        {
                            if (!(activebutton == (maxButtons - 1)) && buttons[activebutton + 1].avaiable)
                            {
                                cursorSound.Play();
                                buttons[activebutton].selected = false;
                                activebutton++;
                                buttons[activebutton].selected = true;

                            }
                        }
                    }
                    else if (keyboardState.IsKeyDown(Keys.Right))
                    {
                        if (!previousKeyboardState.IsKeyDown(Keys.Right))
                        {
                            if (!(activebutton == 0) && buttons[activebutton - 1].avaiable)
                            {
                                cursorSound.Play();
                                buttons[activebutton].selected = false;
                                activebutton--;
                                buttons[activebutton].selected = true;

                            }
                        }
                    }

                    if ((keyboardState.IsKeyDown(Keys.Enter) || keyboardState.IsKeyDown(Keys.Space))&& !enterLock)
                    {
                        //Submenu aufrufen
                        escapeLock = true;
                        enterLock = true;
                        subMenu1 = new SubMenu1(subMenuSprites, buttons[activebutton].position, scoreFont,buttons[activebutton].scores,buttons[activebutton].name);
                        menuState = 4;
                    }
                }

                
                if (menuState == 3) 
                {

                }
                //Submenu geoeffnet
                if (menuState == 4) 
                {
                    subMenu1.Update(gameTime);
                    if (subMenu1.action == 1) 
                    {
                        if (activebutton == 0)
                        {
                            gameShell = new GameShell(contentSpinGame, 1, scoreFont, buttons[activebutton].scores[0]);
                            isGameRunning = true;
                        }
                    }
                    if (subMenu1.action == 3) 
                    {
                        menuState = 2;
                        escapeLock = true;
                        enterLock = true;
                    }

                }
                
            }
            //End of Update Logik for MainMenu

            //Game Update Logik
            if (isGameRunning) 
            {
                if (loopMusik) 
                {
                    soundEngineInstance.Stop();
                    loopMusik = false;
                }

                gameShell.Update(gameTime);
                if (gameShell.want2quit)
                {
                    buttons[activebutton].scores[0] = gameShell.highScore;
                    isGameRunning = false;
                    escapeLock = true;
                    enterLock = true;
                    menuState = 2;
                }

            }
            
            // TODO: Add your update logic here
            base.Update(gameTime);
            
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

            //Map Menu Dawing
            if (!isGameRunning)
            {
                spriteBatch.Draw(backgroundTexture, viewportRect, Color.White);

                for (int i = 0; i < buttons.Length; i++)
                {
                    buttons[i].draw(spriteBatch, gameTime);
                }
                if (menuState == 3)
                {
                   
                }
                if (menuState == 4) 
                {
                    subMenu1.Draw(spriteBatch, gameTime);
                }
            }
            //End of Map Menu Drawing

            //GameDrawing
            if (isGameRunning)
            {
                gameShell.Draw(spriteBatch, gameTime);
            }
            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
